package cn.edu.zjicm.wordsnet_d.activity;

import android.app.Activity;
import android.content.Context;
import android.content.Intent;
import android.os.Bundle;
import android.support.v4.view.PagerAdapter;
import android.support.v4.view.ViewPager;
import android.view.KeyEvent;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.ViewGroup;
import android.view.Window;
import android.view.WindowManager;
import android.widget.Button;
import android.widget.ImageView;
import android.widget.ImageView.ScaleType;
import android.widget.LinearLayout;
import cn.edu.zjicm.wordsnet_d.R;
import cn.edu.zjicm.wordsnet_d.datapersistent.AppPreference;
import cn.edu.zjicm.wordsnet_d.view.PageControl;

import com.umeng.analytics.MobclickAgent;

public class GuideActivityFull extends Activity implements OnClickListener {
	public static void startActivity(Context context) {
		Intent i = new Intent(context, GuideActivityFull.class);
		context.startActivity(i);
	}

	private ViewPager mViewPager;
	private PageControl dot;
	private Button startBtn;

	private int[] guideDrawables;

	public static GuideActivityFull mInstance;

	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		mInstance = GuideActivityFull.this;
		requestWindowFeature(Window.FEATURE_NO_TITLE);
		getWindow().setFlags(WindowManager.LayoutParams.FLAG_FULLSCREEN, WindowManager.LayoutParams.FLAG_FULLSCREEN);
		setContentView(R.layout.activity_guide_full);
		findViews();
		init();
	}

	private void findViews() {
		mViewPager = (ViewPager) findViewById(R.id.flipper);
		dot = (PageControl) findViewById(R.id.dot);
		startBtn = (Button) findViewById(R.id.startBtn);
	}

	private void init() {
		guideDrawables = new int[] { R.drawable.bg1, R.drawable.bg2, R.drawable.bg3 };
		startBtn.setOnClickListener(this);
		mViewPager.setAdapter(new PagerAdapter() {
			@Override
			public boolean isViewFromObject(View arg0, Object arg1) {
				return arg0 == arg1;
			}

			@Override
			public int getCount() {
				return guideDrawables.length;
			}

			@Override
			public void destroyItem(ViewGroup view, int position, Object object) {
				// view.removeView();
			}

			@Override
			public Object instantiateItem(ViewGroup view, int position) {
				ImageView iv = new ImageView(GuideActivityFull.this);
				iv.setScaleType(ScaleType.CENTER_CROP);
				// iv.setBackgroundResource(guideDrawables[position]);
				LinearLayout.LayoutParams ll = new LinearLayout.LayoutParams(LinearLayout.LayoutParams.MATCH_PARENT, LinearLayout.LayoutParams.WRAP_CONTENT);
				// ll.gravity = Gravity.CENTER;
				iv.setLayoutParams(ll);
				iv.setImageResource(guideDrawables[position]);
				view.addView(iv);
				return iv;
			}

		});
		dot.setPointCount(guideDrawables.length);
		dot.setOnPageControlListener(mViewPager, dot);

	}

	@Override
	public boolean onKeyDown(int keyCode, KeyEvent event) {
		if (keyCode == KeyEvent.KEYCODE_BACK) {
			// startNextActivity();
			finish();
			return true;
		}
		return super.onKeyUp(keyCode, event);
	}

	@Override
	protected void onResume() {
		super.onResume();
		MobclickAgent.onResume(this);
	}

	@Override
	protected void onPause() {
		super.onPause();
		MobclickAgent.onPause(this);
	}

	@Override
	public void onClick(View v) {
		if (v == startBtn) {

			if (AppPreference.getLoginID(GuideActivityFull.this) == "guest")
				RegisterActivity.startActivity(GuideActivityFull.this, RegisterActivity.FROM_NEW_USER, RegisterActivity.TO_MAIN);
			else if (AppPreference.getLoginID(GuideActivityFull.this) == "")
				LoginActivity.startActivity(GuideActivityFull.this, false);
			else if (AppPreference.getWordBookIndex(GuideActivityFull.this) == -1 && AppPreference.getPhraseBookIndex(this) == -1)
				SelectBookActivity.mStartActivity(GuideActivityFull.this, SelectBookActivity.INIT_MODE2);
			else {
				MainActivity.startActivity(GuideActivityFull.this);
				AppPreference.setFirstGuidePage(GuideActivityFull.this, false);
				toFinish();
			}
			// } else if (v == freshUser) {
			// SelectBookActivity.mStartActivity(GuideActivityFull.this,
			// SelectBookActivity.INIT_MODE2);
			// } else if (v == toLogin) {
			// LoginActivity.startActivity(GuideActivityFull.this, false,
			// RegisterActivity.TO_OTHER);
		}

	}

	public void toFinish() {
		finish();
		mInstance = null;
	}
}
